package com.yanfan.modbus.frame;


import com.yanfan.modbus.code.FunctionCode;
import com.yanfan.modbus.exception.ModbusTransportException;
import com.yanfan.modbus.frame.abs.ModbusResponse;
import io.netty.buffer.ByteBuf;


public class WriteCoilResponse extends ModbusResponse {
    private int writeOffset;
    private boolean writeValue;

    public WriteCoilResponse(int slaveId) throws ModbusTransportException {
        super(slaveId);
    }

    public WriteCoilResponse(int slaveId, int writeOffset, boolean writeValue) throws ModbusTransportException {
        super(slaveId);
        this.writeOffset = writeOffset;
        this.writeValue = writeValue;
    }

    @Override
    public byte getFunctionCode() {
        return FunctionCode.WRITE_COIL;
    }


    @Override
    protected void writeResponse(ByteBuf buf) {
        buf.writeShort(writeOffset);
        buf.writeShort(writeValue ? 0xff00 : 0);
    }


    @Override
    protected void readResponse(ByteBuf buf) {
        writeOffset = buf.readUnsignedShort();
        writeValue = buf.readUnsignedShort() == 0xff00;
    }


    public int getWriteOffset() {
        return writeOffset;
    }


    public boolean isWriteValue() {
        return writeValue;
    }
}
